package com.hechang.controller;

import java.io.*;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.hechang.constant.API;
import com.hechang.util.HttpUtil;
import com.hechang.vo.JsonVo;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.hechang.constant.SystemConstant;
import com.hechang.dto.User;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/hechang/house_source")
public class HouseSourceController {

    @RequestMapping("/list.htm")
    public String login(ModelMap modelMap, HttpServletRequest req) {
    	User user = (User)req.getSession().getAttribute(SystemConstant.SESSION_USER_KEY);
        HttpSession session = req.getSession();
        String sessionId = session.getId();
        modelMap.put("sessionId", sessionId);
        return "house_source/list";
    }



    @ResponseBody
    @RequestMapping(value = "/uploadify.json", method = RequestMethod.POST)
    public JsonVo<String> fileUpload(HttpServletRequest request, HttpServletResponse response) {
        User user = (User)request.getSession().getAttribute(SystemConstant.SESSION_USER_KEY);
        JsonVo<String> json = new JsonVo<String>();
        try {
            MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
            MultipartFile file = null;
            file = multipartRequest.getFile("Filedata");// 获取上传文件名
            String url= API.UPLOAD;
            Map<String, String> params = new HashMap<String, String>();
            params.put("userId", user.getId());
            params.put("busiType", "S_0001");
            String result=HttpUtil.sendPostWithFileStream(url, params, file.getInputStream(), "uploadFile", file.getOriginalFilename());
            json.setMsg(file.getOriginalFilename());
            json.setT(result);
            json.setResult(true);
        } catch (Exception e) {
            json.setResult(false);
            json.setMsg("File upload failed!");
        }
        return json;
    }
}
